/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.verolux.soft.debaraz.controller;

import com.verolux.soft.debaraz.ApplicationException;
import com.verolux.soft.debaraz.bean.Sellout;
import com.verolux.soft.debaraz.service.SelloutService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.WebRequest;

/**
 *
 * @author ut1e8p
 */
@Controller
@RequestMapping("/secure")
public class DeskController {

     private static Logger logger = Logger.getLogger(DeskController.class);

     @Autowired
     private SelloutService selloutService;

    /**
     *
     * @param error
     * @param model
     * @return
     */
    @RequestMapping(value = "/desk.do")
    public String displayDesk(WebRequest request) {
        logger.debug("Received request to show desk page");
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String userId = principal.getUsername();
        try {
            Sellout sellout = selloutService.findByUserId(userId);
            if(sellout != null){
                request.setAttribute("hasSellout", true, WebRequest.SCOPE_REQUEST);
            }
            else{
                request.setAttribute("hasSellout", false, WebRequest.SCOPE_REQUEST);
            }
        } catch (ApplicationException e) {
            logger.error(e.getMessage(), e);
            request.setAttribute("hasSellout", false, WebRequest.SCOPE_REQUEST);
        }
        request.setAttribute("hasSellout", false, WebRequest.SCOPE_REQUEST);
        return "desk";
    }

}
